Imports System.Configuration

Public Class UserControl
    Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents txtName As System.Web.UI.WebControls.TextBox
    Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents ddlCountry As System.Web.UI.WebControls.DropDownList
    Protected WithEvents txtCode As System.Web.UI.WebControls.TextBox
    Protected WithEvents RequiredFieldValidator2 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents txtEmail As System.Web.UI.WebControls.TextBox
    Protected WithEvents RequiredFieldValidator3 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents RegularExpressionValidator1 As System.Web.UI.WebControls.RegularExpressionValidator
    Protected WithEvents txtEANCode As System.Web.UI.WebControls.TextBox
    Protected WithEvents RequiredFieldValidator4 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents txtUserName As System.Web.UI.WebControls.TextBox
    Protected WithEvents RequiredFieldValidator5 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents txtPass As System.Web.UI.WebControls.TextBox
    Protected WithEvents RequiredFieldValidator6 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents CustomValidator1 As System.Web.UI.WebControls.CustomValidator
    Protected WithEvents tr2 As System.Web.UI.HtmlControls.HtmlTableRow
    Protected WithEvents tr3 As System.Web.UI.HtmlControls.HtmlTableRow
    Protected WithEvents tr5 As System.Web.UI.HtmlControls.HtmlTableRow
    Protected WithEvents tr7 As System.Web.UI.HtmlControls.HtmlTableRow
    Protected WithEvents tr8 As System.Web.UI.HtmlControls.HtmlTableRow
    Protected WithEvents lblErr As System.Web.UI.WebControls.Label
    Protected WithEvents CustomValidator2 As System.Web.UI.WebControls.CustomValidator
    Protected WithEvents trCountry As System.Web.UI.HtmlControls.HtmlTableRow
    Protected WithEvents trSortRef1 As System.Web.UI.HtmlControls.HtmlTableRow
    Protected WithEvents trDownloadable As System.Web.UI.HtmlControls.HtmlTableRow
    Protected WithEvents trSortRef2 As System.Web.UI.HtmlControls.HtmlTableRow
    Protected WithEvents txtSortRef1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Requiredfieldvalidator7 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents txtSortRef2 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Requiredfieldvalidator8 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents CompareValidator1 As System.Web.UI.WebControls.CompareValidator
    Protected WithEvents chkDownloadableCSV As System.Web.UI.WebControls.CheckBox
    Protected WithEvents lblPopupTitle As System.Web.UI.WebControls.Label
    Protected WithEvents Tr1 As System.Web.UI.HtmlControls.HtmlTableRow

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

#Region "   Properties"
    Private m_popupTitleEnabled As Boolean

    Public Property PopupTitleEnabled() As Boolean
        Get
            Return m_popupTitleEnabled
        End Get
        Set(ByVal Value As Boolean)
            m_popupTitleEnabled = Value
            If m_popupTitleEnabled Then
                lblPopupTitle.Visible = True
            End If
        End Set
    End Property

    Public Property PageBOSessionName() As String
        Get
            Return CStr(ViewState("PageBOSessionName"))
        End Get
        Set(ByVal Value As String)
            ViewState("PageBOSessionName") = Value
        End Set
    End Property

    Protected Property UserID() As Integer
        Get
            Return PageBO.UsIdPk
        End Get
        Set(ByVal Value As Integer)

        End Set
    End Property
    Protected ReadOnly Property PageBO() As CUser
        Get
            Return CType(Session(Me.PageBOSessionName), CUser)
        End Get
    End Property

#End Region

#Region "   Assign BO's properties"


    'Private Sub txtName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtName.TextChanged
    '    PageBO.Name = txtName.Text
    'End Sub

    'Private Sub txtUserName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtUserName.TextChanged
    '    PageBO.LoginName = txtUserName.Text
    'End Sub

    'Private Sub txtPass_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPass.TextChanged
    '    PageBO.Password = txtPass.Text
    'End Sub

    'Private Sub txtEmail_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtEmail.TextChanged
    '    PageBO.Email = txtEmail.Text
    'End Sub

    'Private Sub ddlCountry_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCountry.SelectedIndexChanged
    '    PageBO.Country = ddlCountry.SelectedItem.Text

    '    If rbUserType.SelectedItem.Text = "Customer" Then
    '        RegularExpressionValidator2.Enabled = True
    '        If ddlCountry.SelectedItem.Value.Equals("USA") Then
    '            RegularExpressionValidator2.ValidationExpression = "\d{6}(-\d{4})?"
    '            RegularExpressionValidator2.ErrorMessage = "EAN/UPC code must be 6 digits long"
    '        Else
    '            RegularExpressionValidator2.ValidationExpression = "\d{7}(-\d{4})?"
    '            RegularExpressionValidator2.ErrorMessage = "EAN/UPC code must be 7 digits long"
    '        End If
    '    Else
    '        RegularExpressionValidator2.Enabled = False
    '    End If
    'End Sub

    'Private Sub rbUserType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbUserType.SelectedIndexChanged
    '    If (rbUserType.SelectedItem.Text.Equals("Internal")) Then
    '        PageBO.UserType = 1
    '    Else
    '        PageBO.UserType = 0 'Customer
    '    End If

    '    If rbUserType.SelectedItem.Text.Equals("Internal") Then
    '        txtName.Enabled = True
    '        txtEmail.Enabled = True
    '        txtUserName.Enabled = True
    '        txtPass.Enabled = True

    '        txtCode.Enabled = False
    '        txtCode.Text = ""
    '        PageBO.CustomerCode = ""
    '        RequiredFieldValidator2.Enabled = False
    '        txtEANCode.Enabled = False
    '        txtEANCode.Text = ""
    '        PageBO.BarCode = ""
    '        RequiredFieldValidator4.Enabled = False
    '        ddlCountry.Enabled = False
    '        ddlCountry.SelectedIndex = -1
    '        PageBO.Country = Nothing
    '    Else
    '        txtName.Enabled = True
    '        txtEmail.Enabled = True
    '        txtUserName.Enabled = True
    '        txtPass.Enabled = True

    '        txtCode.Enabled = True
    '        RequiredFieldValidator2.Enabled = True
    '        txtEANCode.Enabled = True
    '        RequiredFieldValidator4.Enabled = True
    '        ddlCountry.Enabled = True
    '    End If

    'End Sub

    'Private Sub txtCode_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCode.TextChanged
    '    PageBO.CustomerCode = txtCode.Text
    'End Sub

    'Private Sub txtEANCode_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtEANCode.TextChanged
    '    PageBO.BarCode = txtEANCode.Text
    'End Sub
#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        If Not Me.IsPostBack Then
            With PageBO
                txtName.Text = .Name
                txtEmail.Text = .Email
                txtUserName.Text = .LoginName
                txtPass.Text = .Password

                If (.UserType = 0) Then
                    LookupCountry()
                    If (Not .Country Is Nothing) Then
                        ddlCountry.SelectedIndex = ddlCountry.Items.IndexOf(ddlCountry.Items.FindByText(.Country))
                    Else
                        ddlCountry.SelectedIndex = ddlCountry.Items.IndexOf(ddlCountry.Items.FindByValue("US"))
                        .Country = ddlCountry.SelectedItem.Text
                    End If
                    txtCode.Text = .CustomerCode
                    txtCode.ReadOnly = (.UsIdPk > 0)

                    txtEANCode.Text = .BarCode
                    If .UsIdPk > 0 Then
                        txtSortRef1.Text = .SortReference1
                        txtSortRef2.Text = .SortReference2
                    End If
                    chkDownloadableCSV.Checked = .DownloadableCSV

                    trDownloadable.Visible = False
                Else
                    tr2.Visible = False
                    tr3.Visible = False
                    tr5.Visible = False
                    trSortRef1.Visible = False
                    trSortRef2.Visible = False
                    trDownloadable.Visible = False
                End If
            End With

        End If

    End Sub

#Region " Methods"
    Private Sub LookupCountry()
        Dim dt As DataTable = CLookup.GetLookupDataList(Business.Common.GetCountryLKID())
        Dim row As DataRow

        For Each row In dt.Rows
            ddlCountry.Items.Add(New ListItem(CStr(row(2)), CStr(row(3))))
        Next

    End Sub

    Public Sub UpdateData()
        With PageBO
            .Name = txtName.Text.Trim()
            .LoginName = txtUserName.Text.Trim()
            If .Password = String.Empty Then
                .Password = HashPassword(txtPass.Text)
            End If
            .Email = txtEmail.Text.Trim()
            If .UserType = 0 Then
                .CustomerCode = txtCode.Text.Trim()
                .BarCode = txtEANCode.Text.Trim()
                .Country = ddlCountry.SelectedItem.Text.Trim()
                .SortReference1 = txtSortRef1.Text.Trim()
                .SortReference2 = txtSortRef2.Text.Trim()
                .DownloadableCSV = chkDownloadableCSV.Checked
            Else
                '.CustomerCode = ""
                '.BarCode = ""
                '.Country = Nothing
            End If

            .CreatedDate = DateTime.Now
            .IsAdmin = False
            .IsEnabled = True
        End With
    End Sub

    Public Sub SetErrorMessage(ByVal mess As String)
        Me.lblErr.Text = mess
    End Sub
    Public Sub EnableUserNameControl(ByVal value As Boolean)
        tr7.Visible = value
    End Sub

    Public Sub EnablePasswordControl(ByVal value As Boolean)
        tr8.Visible = value
    End Sub

#End Region
End Class
